#include "mathmodedecryptbase.h"
#include <QDebug>
#include <QRegExp>

MathModeDecryptBase::MathModeDecryptBase(MathModeDecryptBase::E_ProgramModeType emProgramModeType,QObject *parent) : QObject(parent)
{
    this->emProgramModeType = emProgramModeType;
    switch(this->emProgramModeType)
    {
    case E_ProgramModeType_MAIN://
    {
        QMap<char,char> checkNumber;

        checkNumber.insert('0','6');
        checkNumber.insert('1','4');
        checkNumber.insert('2','8');
        checkNumber.insert('3','9');
        checkNumber.insert('4','2');
        checkNumber.insert('5','5');
        checkNumber.insert('6','0');
        checkNumber.insert('7','3');
        checkNumber.insert('8','7');
        checkNumber.insert('9','1');

        QMap<int,char> checkRandNumber;
        checkRandNumber.insert(10,'Y');
        checkRandNumber.insert(11,'I');
        checkRandNumber.insert(12,'B');
        checkRandNumber.insert(13,'P');
        checkRandNumber.insert(14,'U');
        checkRandNumber.insert(15,'W');
        checkRandNumber.insert(16,'D');
        checkRandNumber.insert(17,'T');
        checkRandNumber.insert(18,'J');
        checkRandNumber.insert(19,'R');

//        checkRandNumber.insert(10,'Y');
//        checkRandNumber.insert(11,'B');
//        checkRandNumber.insert(12,'I');
//        checkRandNumber.insert(13,'P');
//        checkRandNumber.insert(14,'W');
//        checkRandNumber.insert(15,'U');
//        checkRandNumber.insert(16,'D');
//        checkRandNumber.insert(17,'T');
//        checkRandNumber.insert(18,'R');
//        checkRandNumber.insert(19,'J');

        //17-33
        QMap<char,int> checkCharacterAHQLM;

//        checkCharacterAHQLM.insert('A',17);
//        checkCharacterAHQLM.insert('H',32);
//        checkCharacterAHQLM.insert('Q',24);
//        checkCharacterAHQLM.insert('L',30);
//        checkCharacterAHQLM.insert('M',29);

        checkCharacterAHQLM.insert('A',17);
        checkCharacterAHQLM.insert('H',32);
        checkCharacterAHQLM.insert('Q',24);
        checkCharacterAHQLM.insert('L',30);
        checkCharacterAHQLM.insert('M',29);

        setCheckNumber(checkNumber);
        setCheckRandNumber(checkRandNumber);
        setCheckCharacterAHQLM(checkCharacterAHQLM);
    }
        break;
    default:
    {

    }
        break;
    }
}

QString MathModeDecryptBase::getNum(QString s)
{
    QString pattern("\\s*([A-Z0-9]{4})\\w([A-Z0-9]{4})\\w([A-Z0-9]{4})\\w([A-Z0-9]{4})\\w([A-Z0-9]{4})\\w([A-Z0-9]{4})\\w\\s*");//加密号
    QRegExp rx(pattern);
    s.indexOf(rx);
    /**提取关键字**/
    QString serialNum = rx.cap(1)+rx.cap(2)+rx.cap(3);
    qDebug()<<serialNum;
    /**拆分个体**/
    QByteArray ba1 = serialNum.toLatin1();
    char *charNum = ba1.data();
    /**逐一还原**/
    //strDecry decry;
    char p1[13] = {0};
    p1[0] = checkNumber(charNum[0]);
    for(int i = 1; i < 4;i++)
    {
        p1[i] =  checkCharacterA(charNum[i]);
    }
    for(int i = 4; i < 6;i++)
    {
        p1[i] = checkCharacterH(charNum[i]);
    }
    p1[6] = checkNumber(charNum[6]);
    p1[7] = checkCharacterH(charNum[7]);
    p1[8] = checkNumber(charNum[8]);

    for(int i = 9; i < 12;i++)
    {
        p1[i] = checkCharacterQ(charNum[i]);
    }
    p1[12] = '\0';

//    for(int i = 0; i < 13;i++)
//    {
//        qDebug() << "i = **"<<i<<"**p1[i]*"<<p1[i]<<endl;
//    }

    QString strNum = QString(QLatin1String(p1));
    qDebug() << strNum;

    return strNum;
}

QString MathModeDecryptBase::getDate(QString in)
{
    QString pattern("\\s*([A-Z0-9]{4})\\w([A-Z0-9]{4})\\w([A-Z0-9]{4})\\w([A-Z0-9]{4})\\w([A-Z0-9]{4})\\w([A-Z0-9]{4})\\w\\s*");//加密号
    QRegExp rx(pattern);
    in.indexOf(rx);
    /**提取关键字**/
    QString serialDate = rx.cap(4)+rx.cap(5);
    qDebug()<<serialDate;
    /**拆分个体**/
    QByteArray ba2  = serialDate.toLatin1();
    char *charDate = ba2.data();
    /**逐一还原**/
    //strDecry decry;
    char p2[9];
    for(int i = 0; i < 2;i++)
    {
        p2[i] = checkCharacterL(charDate[i]);
    }
    for(int i = 2; i < 4;i++)
    {
        p2[i] = checkNumber(charDate[i]);
    }
    for(int i = 4; i < 8;i++)
    {
        p2[i] = checkCharacterM(charDate[i]);
    }
    p2[8] = '\0';

//    for(int i = 0; i < 9;i++)
//    {
//        qDebug() << "i = **"<<i<<"**p2[i]*"<<p2[i]<<endl;
//    }

    QString strDate = QString(QLatin1String(p2));

    qDebug() << "**strDate**"<<strDate;

    return strDate;
}

char MathModeDecryptBase::checkNumber(char n)
{
    QMap<char,char>::iterator itemMap;
    itemMap = mapCheckNumber.find(n);
    if(itemMap != mapCheckNumber.end())
    {

        return itemMap.value();
    }
    else
    {
        return '#';
    }
}

char MathModeDecryptBase::checkCharacterA(char c)
{
    QMap<char,int>::iterator iterMapCheckCharacterAHQLM;
    char characterFlag='A';
    iterMapCheckCharacterAHQLM = mapCheckCharacterAHQLM.find(characterFlag);
    if(iterMapCheckCharacterAHQLM != mapCheckCharacterAHQLM.end())
    {
        int valueData =mapCheckCharacterAHQLM.value(characterFlag);
        return c-valueData;
    }
    else
    {
        return c-17;
    }
}

char MathModeDecryptBase::checkCharacterH(char c)
{
    QMap<char,int>::iterator iterMapCheckCharacterAHQLM;
    char characterFlag='H';
    iterMapCheckCharacterAHQLM = mapCheckCharacterAHQLM.find(characterFlag);
    if(iterMapCheckCharacterAHQLM != mapCheckCharacterAHQLM.end())
    {
        int valueData =mapCheckCharacterAHQLM.value(characterFlag);
        return c-valueData;

    }
    else
    {
        return c-32;
    }
}

char MathModeDecryptBase::checkCharacterQ(char c)
{
    QMap<char,int>::iterator iterMapCheckCharacterAHQLM;
    char characterFlag='Q';
    iterMapCheckCharacterAHQLM = mapCheckCharacterAHQLM.find(characterFlag);
    if(iterMapCheckCharacterAHQLM != mapCheckCharacterAHQLM.end())
    {
        int valueData =mapCheckCharacterAHQLM.value(characterFlag);
        return c-valueData;
    }
    else
    {
        return c-24;
    }
}

char MathModeDecryptBase::checkCharacterL(char c)
{
    QMap<char,int>::iterator iterMapCheckCharacterAHQLM;
    char characterFlag='L';
    iterMapCheckCharacterAHQLM = mapCheckCharacterAHQLM.find(characterFlag);
    if(iterMapCheckCharacterAHQLM != mapCheckCharacterAHQLM.end())
    {
        int valueData =mapCheckCharacterAHQLM.value(characterFlag);
        return c-valueData;
    }
    else
    {
        return c-30;
    }
}

char MathModeDecryptBase::checkCharacterM(char c)
{
    QMap<char,int>::iterator iterMapCheckCharacterAHQLM;
    char characterFlag='M';
    iterMapCheckCharacterAHQLM = mapCheckCharacterAHQLM.find(characterFlag);
    if(iterMapCheckCharacterAHQLM != mapCheckCharacterAHQLM.end())
    {
        int valueData =mapCheckCharacterAHQLM.value(characterFlag);
        return c-valueData;
    }
    else
    {
        return c-29;
    }
}

int MathModeDecryptBase::setCheckNumber(QMap<char,char>& mapCheckNumber)
{
    QMap<char,char>::iterator iterMap;
    iterMap=mapCheckNumber.begin();
    while(iterMap!=mapCheckNumber.end())
    {
        this->mapCheckNumber.insert(iterMap.value(),iterMap.key());
        iterMap++;
    }
    return 0;
}
int MathModeDecryptBase::setCheckRandNumber(QMap<int,char>& mapCheckRandNumber)
{
    QMap<int,char>::iterator iterMap;
    iterMap=mapCheckRandNumber.begin();
    while(iterMap!=mapCheckRandNumber.end())
    {
        this->mapCheckRandNumber.insert(iterMap.value(),iterMap.key());
        iterMap++;
    }

    return 0;
}

int MathModeDecryptBase::setCheckCharacterAHQLM(QMap<char,int>& mapCheckCharacterAHQLM)
{
    QMap<char,int>::iterator iterMap;
    iterMap=mapCheckCharacterAHQLM.begin();
    while(iterMap!=mapCheckCharacterAHQLM.end())
    {
        this->mapCheckCharacterAHQLM.insert(iterMap.key(),iterMap.value());
        iterMap++;
    }
    return 0;
}

